package com.ruoyi.project.park.controller;


//统计

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.park.domain.BizEntryRecord;
import com.ruoyi.project.park.domain.BizFeeRecord;
import com.ruoyi.project.park.domain.BizParking;
import com.ruoyi.project.park.domain.BizParkingCard;
import com.ruoyi.project.park.service.impl.BizEntryRecordServiceImpl;
import com.ruoyi.project.park.service.impl.BizFeeRecordServiceImpl;
import com.ruoyi.project.park.service.impl.BizParkingCardServiceImpl;
import com.ruoyi.project.park.service.impl.BizParkingServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/park/api")
public class DemoController extends BaseController {
    @Autowired
    private BizParkingCardServiceImpl bizParkingCardService;
    @Autowired
    private BizParkingServiceImpl parkingService;
    @Autowired
    private BizEntryRecordServiceImpl entryRecordService;
    @Autowired
    private BizFeeRecordServiceImpl bizFeeRecordService;
    //统计数量
    @RequestMapping("/count")
    public AjaxResult countApi() {
    /*
    会员数量 车位数量 进出数量 订单数量
     */

        HashMap<String, Integer> hashMap = new HashMap<>();
        int size1 = bizParkingCardService.selectBizParkingCardList(new BizParkingCard()).size();
        int size2 = parkingService.selectBizParkingList(new BizParking()).size();
        int size3 = entryRecordService.selectBizEntryRecordList(new BizEntryRecord()).size();
        int size4 = bizFeeRecordService.selectBizFeeRecordList(new BizFeeRecord()).size();
        hashMap.put("userNum",size1);
        hashMap.put("parkNum",size2);
        hashMap.put("entryNum",size3);
        hashMap.put("orderNum",size4);
        return AjaxResult.success(hashMap);
    }
    @RequestMapping("/count2")
    public AjaxResult countApi2() {
    //查询所有进出记录
        List<BizEntryRecord> recordList = entryRecordService.selectBizEntryRecordList(new BizEntryRecord());

        //1进站 2 出站
        //进站的
        List<BizEntryRecord> list1 = recordList.stream().filter(record -> record.getEntryType().equals("1")).collect(Collectors.toList());

        //出站的
        List<BizEntryRecord> list2 = recordList.stream().filter(record -> record.getEntryType().equals("2")).collect(Collectors.toList());

        //6天前 5天前 4天前 3天前 2天前 1天前 今天
        //list1中统计出6天前 5天前 4天前 3天前 2天前 1天前 今天 按照creatTime 的数量
        LocalDate today = LocalDate.now();

        // 初始化一个 Map 用于存储每天的数量统计结果
        Map<String, Long> countsByDay1 = new LinkedHashMap<>();

        // 遍历过去6天到今天
        for (int i = 0; i <= 6; i++) {
            LocalDate targetDay = today.minusDays(i);
            String dayString = targetDay.toString();

            //Date 转 LocalDate

            //LocalDateTime targetDateTime = LocalDateTime.of(targetDay, LocalTime.MIN);
            long count = list1.stream()
                    .filter(obj -> isWithinDay(obj.getCreateTime(), targetDay))
                    .count();

            countsByDay1.put(dayString, count);
        }

        // 初始化一个 Map 用于存储每天的数量统计结果
        Map<String, Long> countsByDay2 = new LinkedHashMap<>();

        // 遍历过去6天到今天
        for (int i = 0; i <= 6; i++) {
            LocalDate targetDay = today.minusDays(i);
            String dayString = targetDay.toString();

            //Date 转 LocalDate

            //LocalDateTime targetDateTime = LocalDateTime.of(targetDay, LocalTime.MIN);
            long count = list2.stream()
                    .filter(obj -> isWithinDay(obj.getCreateTime(), targetDay))
                    .count();

            countsByDay2.put(dayString, count);
        }

        List<Long> val1 = new ArrayList<>(countsByDay1.values());
        //val1 顺序颠倒一下
       // val1 = val1.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
        //val1倒序
        //有错误
        Collections.reverse(val1);

       // val1 = val1.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
        List<Long> val2 = new ArrayList<>(countsByDay2.values());
        //val2倒序
        //val2 = val2.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
        Collections.reverse(val2);

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("in",val1);
        hashMap.put("out",val2);

        //排序
        //val1.sort(Comparator.reverseOrder());


        return AjaxResult.success(hashMap);

    }
    private static boolean isWithinDay(Date createTime, LocalDate targetDay) {
        //createTIme转换为LocalDate
        LocalDate createTimeLocalDate = createTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        return createTimeLocalDate.equals(targetDay);
    }
}
